<?php
/*
	[Destoon B2B System] Copyright (c) 2008-2015 www.destoon.com
	This is NOT a freeware, use is subject to license.txt
*/
defined('IN_DESTOON') or exit('Access Denied');
if($itemid) {
	$item = $db->get_one("SELECT itemid,title,username,company,groupid,truename,telephone,mobile,areaid,catid,address,hits,thumb,email,qq,addtime,edittime,linkurl,status FROM {$table} WHERE itemid=$itemid");
	($item && $item['status'] > 2) or appexit($L['msg_not_exist']);
	extract($item);
	$CAT = get_cat($catid);
	$headname = $CAT['catname'];
	//echo $head_name;
	if(!check_group($_groupid, $MOD['group_show']) || !check_group($_groupid, $CAT['group_show'])) mobile_msg($L['msg_no_right']);
	$member = array();
	$fee = get_fee($item['fee'], $MOD['fee_view']);
	include 'contact.inc.php';
	$content_table = content_table($moduleid, $itemid, $MOD['split'], $table_data);
	$t = $db->get_one("SELECT content FROM {$content_table} WHERE itemid=$itemid");
	$content = video5($t['content']);
	$editdate = timetodate($edittime, 5);
	$could_purchase = (SELL_ORDER && $price > 0 && $minamount > 0 && $amount > 0 && $unit && $username && $username != $_username) ? 1 : 0;
	$could_inquiry = ($user_status == 3 && $username && $username != $_username) ? 1 : 0;
	$update = '';
	include DT_ROOT.'/include/update.inc.php';
	$userid = get_user($item['username'], 'username', 'userid');
	$item['headname'] = $headname;
	$item['content'] = $content;
	$item['area'] = area_pos($item['areaid'], '&nbsp;');
	$item['career'] = $member['career'];
	$item['adddate'] = timetodate($item['addtime'], 5);
	$item['editdate'] = timetodate($item['edittime'], 5);
	$item['comlon'] = $compoint['0'];
	$item['comlat'] = $compoint['1'];
	$item['commentCount'] = get_numcomment($moduleid,$itemid);
	appjson($item);
} else {
	if($kw) {
		check_group($_groupid, $MOD['group_search']) or mobile_msg($L['msg_no_search']);
	} else if($catid) {
		$CAT or mobile_msg($L['msg_not_cate']);
		if(!check_group($_groupid, $MOD['group_list']) || !check_group($_groupid, $CAT['group_list'])) {
			mobile_msg($L['msg_no_right']);
		}
	} else {
		check_group($_groupid, $MOD['group_index']) or mobile_msg($L['msg_no_right']);
	}
	$head_title = $MOD['name'].$DT['seo_delimiter'].$head_title;
	if($kw) $head_title = $kw.$DT['seo_delimiter'].$head_title;
	$condition = "status=3";
	//$condition .= ' GROUP BY username ';
	if($keyword) $condition .= " AND keyword LIKE '%$keyword%'";
	//$CAT = get_cat($catid);
	if($catid) $condition .= $CAT ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
	if($areaid) $condition .= $ARE['child'] ? " AND areaid IN (".$ARE['arrchildid'].")" : " AND areaid=$areaid";
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $condition", 'CACHE');
	$items = $r['num'];
	if($_GET['pagesize']) $pagesize = $_GET['pagesize'];
	$pages = mobile_pages($items, $page, $pagesize);
	$lists = array();
	if($items) {
		$order = $MOD['order'];
		$time = strpos($MOD['order'], 'add') !== false ? 'addtime' : 'edittime';
		$result = $db->query("SELECT itemid,username,title,thumb,company,groupid,hits,addtime,edittime FROM {$table} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $db->fetch_array($result)) {
			//if($kw) $r['title'] = str_replace($kw, '<b class="f_red">'.$kw.'</b>', $r['title']);
			$CAT = get_cat($r['catid']);
	        $r['headname'] = utftext($CAT['catname']);
			$r['commentCount'] = get_numcomment('5',$r['itemid']);
			$r['title'] = utftext(dsubstr($r['title'],30));
			$r['company'] = utftext($r['company']);
			$r['mobile'] = dsubstr(trim($r['mobile']), 11);
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$lists[] = $r;
		}
		$db->free_result($lists);
	}
	
	$totalCount = $items;
	$totalPages = ceil($items/$pagesize);
	$datalist = array('data' => $lists,'totalCount' => $totalCount,'totalPages' => $totalPages);

	exit(json_encode($datalist));
	$back_link = mobileurl($moduleid);
	
}
include template($module, 'mobile');
?>